package com.zhongjian.technologycal.mall.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wei on 2018/3/16.
 */

public class OrderAdapter extends FragmentPagerAdapter{

    private String[] title;
    private List<Fragment> list;
    private FragmentManager fm;

    public OrderAdapter(FragmentManager fm, String[] title, List<Fragment> views) {
        super(fm);
        this.fm = fm;
        this.title = title;
        this.list = views;
    }


    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }


    //配置标题的方法
    @Override
    public CharSequence getPageTitle(int position) {
        return title[position];
    }

    public void setFragments(ArrayList<Fragment> fragments) {
        if ((list == null) && (fm == null) || (fragments == null)) {
//            LogUtil.w(TAG, "setFragments is fail. params is null");
            return;
        }
        FragmentTransaction ft = fm.beginTransaction();
        for (int i = 0; i < list.size(); i++) {
            Fragment fragment = list.get(i);
            ft.remove(fragment);
        }
        ft.commit();
        ft = null;
        fm.executePendingTransactions();
        this.list.addAll(fragments);
        notifyDataSetChanged();
    }
}
